package org.example.server.goods.listener.mq.kafka;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.example.frw.canal.EntityComposite;
import org.example.frw.canal.HashValue;
import org.example.frw.canal.binlog.BaseCanalBinlog;
import org.example.frw.canal.binlog.entity.GoodsParamBinlogEntity;
import org.example.frw.canal.config.TopicConcurrentlySetting;
import org.example.frw.canal.listener.mq.kafka.BaseConsumer;
import org.example.server.goods.entity.snapshot.GoodsParamSnapshotEntity;
import org.example.server.goods.mapper.snapshot.GoodsParamSnapshotMapper;
import org.example.server.goods.mapstruct.snapshot.param.GoodsParamSnapshotConverter;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.listener.BatchAcknowledgingMessageListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 保存快照
 */
@Component
@Slf4j
public class GoodsParamConsumer extends BaseConsumer<GoodsParamBinlogEntity> implements BatchAcknowledgingMessageListener<String, String> {
    private static final String TOPIC = "goods__tb_goods_param";
    private static final String GROUP = "insert_into_snapshot";

    private static final CopyOptions copyProperties = CopyOptions.create().setIgnoreNullValue(true);

    @Resource
    private GoodsParamSnapshotConverter goodsParamSnapshotConverter;

    @Resource
    private GoodsParamSnapshotMapper goodsParamSnapshotMapper;


    public GoodsParamConsumer(TopicConcurrentlySetting topicConcurrentlySetting){
        super(topicConcurrentlySetting, TOPIC, GROUP);
    }

    /**
     * @param entityComposite entityComposite
     */
    @Override
    @SneakyThrows
    protected void doProcess(EntityComposite<? extends HashValue> entityComposite) {
        log.info("GoodsParamConsumer processing index = {},entityComposite={}",entityComposite.getPartition(), entityComposite);
        GoodsParamBinlogEntity goodsParamEntity = (GoodsParamBinlogEntity) entityComposite.getEntity();
        switch (entityComposite.getType()) {
            case "UPDATE" -> {
                GoodsParamBinlogEntity oldEntity = (GoodsParamBinlogEntity) entityComposite.getOldEntity();
                BeanUtil.copyProperties(oldEntity,goodsParamEntity,copyProperties);
                GoodsParamSnapshotEntity snapshot = goodsParamSnapshotConverter.toGoodsParamSnapshotEntity(goodsParamEntity);
                goodsParamSnapshotMapper.insert(snapshot);
            }
            case "INSERT" -> {
                GoodsParamSnapshotEntity snapshot = goodsParamSnapshotConverter.toGoodsParamSnapshotEntity(goodsParamEntity);
                goodsParamSnapshotMapper.insert(snapshot);
            }
        }
        log.info("processing done！！！");
    }

    @Override
    protected List<EntityComposite<GoodsParamBinlogEntity>> hashIndex(int concurrentlyLevel, ConsumerRecord<String, String> messageExt) {
        log.info("再次分区，concurrentlyLevel={},key={},partition={},offset={}", concurrentlyLevel, messageExt.key(), messageExt.partition(), messageExt.offset());
        String body = messageExt.value();
        TypeReference<BaseCanalBinlog<GoodsParamBinlogEntity>> typeReference = new TypeReference<>() {
        };
        BaseCanalBinlog<GoodsParamBinlogEntity> bean = JSONUtil.toBean(body, typeReference, true);
        return getEntityComposites(concurrentlyLevel, bean);
    }

    @Override
    @KafkaListener(topics = TOPIC, groupId = GROUP, containerFactory = BATCH_CONTAINER_FACTORY)
    public void onMessage(List<ConsumerRecord<String, String>> records, Acknowledgment acknowledgment) {
        super.consumeMessage(records,acknowledgment);
    }
}